package com.wx.websocket.action;

import com.wx.websocket.entity.User;
import com.wx.websocket.service.LoginService;
import com.wx.websocket.util.ResultUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;

/**
 * Created by Administrator on 2017/1/2.
 */
@Component
@Path("/user")
@Scope("prototype")
@Produces({"application/json"})
public class LoginAction {

    @Resource
    private LoginService loginService;

    @GET
    @Path("/list")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String getAllUser( @QueryParam("jsonpcallback") String jsonpcallback){
        List<User> users = loginService.getAllUsers();
//        for(User u:users){
//            result.append(u.toString()+"/n");
//        }
        String result = jsonpcallback+"("+ ResultUtil.resultStr(1,true,users.get(0))+")";
        return result;
    }

    @GET
    @Path("/login/{userName}/{passWord}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String login(@QueryParam("userName") String userName,
                        @QueryParam("passWord") String passWord,
                        @QueryParam("jsonpcallback") String jsonpcallback){
        User user = loginService.userLogin(userName,passWord);
        String result = "";
        if(user!=null){
            // 说明用户登陆成功
             result = jsonpcallback+"("+ ResultUtil.resultStr(1,true,user)+")";
        }else{
            // 如果为null，说明用户登陆不成功
            result = jsonpcallback+"("+ ResultUtil.resultStr(0,false,"用户名或密码错误")+")";
        }

        return result;
    }
}
